package org.uranus.consul.provider.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.uranus.consul.provider.entity.User;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务提供者
 */
@RestController
public class UserController {
	
	@GetMapping("/getUsers")
	public List<User> getUsers(){
		List<User> users=new ArrayList<>();
		users.add(new User(1,"张三"));
		users.add(new User(2,"王五"));
		users.add(new User(3,"李四"));
		return users;
	}
	/**
	 * 传参测试
	 * @param id
	 * @return
	 */
	@GetMapping("/getUser/{id}")
	public User getUser(@PathVariable Integer id) {
		List<User> users=new ArrayList<>();
		users.add(new User(1,"张三"));
		users.add(new User(2,"王五"));
		users.add(new User(3,"李四"));
		
		for(User user:users) {
			if(user.getId()==id) {
				return user;
			}
		}
		return null;
	}

	@PostMapping("/json")
	public JSONObject findOne(@RequestBody JSONObject json){
		System.out.println("访问生产");
		System.out.println(json);
		User user=new User();
		user.setId(111);
		user.setName("张三");
		return (JSONObject) JSONObject.toJSON(user);
	}

}
